package com.guang.syncronized;

import com.guang.syncronized.domain.A;
import org.openjdk.jol.info.ClassLayout;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.TimeUnit;

/**
 * @Description 升级成为重量级锁  首先设置偏向锁延迟为0 -XX:BiasedLockingStartupDelay=0
 * @Author liguang
 * @Date 2022/10/19/10:36
 */
public class SyncronziedDemoSeven {
    private final static Logger logger = LoggerFactory.getLogger(SyncronziedDemoSeven.class);

    public static void main(String[] args) {

        A lock  = new A();
        logger.info("当前锁状态是无锁可偏向状态 101 无任何偏向");
        logger.info(ClassLayout.parseInstance(lock).toPrintable());
        Thread t1 = new Thread(() -> {
            synchronized (lock) {
                try {
                    logger.info("状态锁状态是偏向锁，101，偏向t1");
                    logger.info(ClassLayout.parseInstance(lock).toPrintable());
                    TimeUnit.SECONDS.sleep(3);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }, "t1");

        t1.start();

        try {
            TimeUnit.MICROSECONDS.sleep(10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        Thread t3 = new Thread(() -> {
            synchronized (lock) {
                try {
                    logger.info("状态锁状态是重量级锁，010");
                    logger.info(ClassLayout.parseInstance(lock).toPrintable());
                    TimeUnit.MICROSECONDS.sleep(10);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }, "t3");
        t3.start();
    }
}
